﻿using System;
using System.Collections.Generic;
using System.Text;
using JRBase;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace JRTestGame
{
    public static class MapCreator
    {

        public static Map createFirstMap(Player player, ContentManager content, 
            int screenWidth, int screenHeight, int mapWidth, int mapHeight, float graffity)
        {
            Map map = new Map();
            MapHandler mapHandler = new MapHandler();
            mapHandler.Initialize(map);
            map.Initialize(content.Load<Texture2D>("map"), player, screenWidth, screenHeight,
               mapWidth, mapHeight, 0, 0, graffity, mapHandler);
            createFirstBoxes(content, map);
            //createFirstEnemys(content, map);

            return map;
        }

        private static void createFirstEnemys(ContentManager content, Map map)
        {
            int enemyWidth = 47;
            int enemyHeight = 61;
            int x = 600;
            int y = map.Height - 220;
            Enemy enemy1 = new Enemy();
            enemy1.Initialize(content.Load<Texture2D>("mineAnimation"),
                new Rectangle(x, y, enemyWidth, enemyHeight), 8, 5);

            x = 1200;
            y = map.Height - 320;
            Enemy enemy2 = new Enemy();
            enemy2.Initialize(content.Load<Texture2D>("mineAnimation"),
                new Rectangle(x, y, enemyWidth, enemyHeight), 8, 5);

            map.Enemies.Add(enemy1);
            map.Enemies.Add(enemy2);
        }

        private static void createFirstBoxes(ContentManager content, Map map)
        {
            int boxWidth = 60;
            int boxHeight = 60;

            bool up = true;
            int step = 0;
            for (int i = 0; i < map.Width / boxWidth; i++)
            {
                bool createE = false;
                if (i != 0 && i % 10 == 0)
                {
                    createE = true;
                    if (up)
                        step += 100;
                    else
                        step -= 100;
                }
                int x = boxWidth * i;
                int y = map.Height - boxHeight - step;
                if (y - map.Player.Sprite.Height - 100 < 0)
                {
                    up = false;
                }

                if (y == map.Height - boxHeight)
                {
                    up = true;
                }

                Box box = new Box();
                box.Initialize(content.Load<Texture2D>("box"),
                    new Rectangle(x, y, boxWidth, boxHeight));

                map.Boxes.Add(box);

                if (createE)
                {
                    createEnemy(x, y, content, map);
                }
            }
        }

        public static void createEnemy(int x, int y, ContentManager content, Map map)
        {
            int enemyWidth = 47;
            int enemyHeight = 61;
            y = y - enemyHeight;
            Enemy enemy1 = new Enemy();
            enemy1.Initialize(content.Load<Texture2D>("mineAnimation"),
                new Rectangle(x, y, enemyWidth, enemyHeight), 8, 5);

            map.Enemies.Add(enemy1);
        }
    }
}
